﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Chaos.view.powersheet
{
    /// <summary>
    /// Interaction logic for powermarker.xaml
    /// </summary>
    public partial class powermarker : UserControl
    {

        public Point DragStart { get; set; }
        public bool IsDragging { get; set; }

        public powermarker()
        {
            InitializeComponent();
        }


        private drag.manager FindDragManager(UIElement uielement)
        {
            DependencyObject parent = uielement as DependencyObject;
            while (parent != null)
            {
                if (parent.GetType() == typeof(drag.manager))
                {
                    drag.manager m = parent as drag.manager;
                    return m;
                }
                parent = VisualTreeHelper.GetParent(parent);
            }
            return null;
        }


        private void powermarker_control_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            UIElement uielement = sender as UIElement;
            if (e.LeftButton == MouseButtonState.Pressed && !IsDragging)
            {
                Point position = e.GetPosition(uielement);

                if (Math.Abs(position.X - DragStart.X) > SystemParameters.MinimumHorizontalDragDistance ||
                    Math.Abs(position.Y - DragStart.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    IsDragging = true;
                    FindDragManager(uielement).ExecuteDrag(uielement, e);
                    IsDragging = false;
                }
            } 
        }

        private void powermarker_control_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            UIElement uielement = sender as UIElement;
            DragStart = e.GetPosition(uielement);
            e.Handled = true;
        }


        public String Faction
        {
            get
            {
                return (String)GetValue(FactionProperty);
            }
            set
            {
                SetValue(FactionProperty, value);
            }
        }

        public static readonly DependencyProperty FactionProperty =
            DependencyProperty.Register(
                "Faction",
                typeof(String),
                typeof(powermarker),
                new PropertyMetadata(default(String), OnFactionPropertyChanged));

        private static void OnFactionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // AutocompleteTextBox source = d as AutocompleteTextBox; 
            // Do something... 
        } 

    }
}
